package com.nono.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nono.Dao.CommentDao;
import com.nono.Dao.UserDao;
import com.nono.service.UserService;

@Controller
public class UserController {
	@Autowired
	UserDao userDao;
	
	@Autowired
	UserService userService;
	
	@Autowired
	CommentDao commentDao;
	
	@RequestMapping(value = "reg", method = RequestMethod.POST)
	@ResponseBody
	public boolean reg(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String defaultAddress = request.getParameter("defaultAddress");
		String defaultPhone = request.getParameter("defaultPhone");
		String mail = request.getParameter("mail");
		return userDao.reg(name, password, defaultAddress, defaultPhone, mail);
	}
	
	/*
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public boolean login(HttpServletRequest request, HttpServletResponse response) {
		return userDao.login( request.getParameter("name"), request.getParameter("password"));
	}
	*/
	
	@RequestMapping(value = "addOrder", method = RequestMethod.POST)
	@ResponseBody
	public boolean addOrder(HttpServletRequest request, HttpServletResponse response) {
		int userId = Integer.parseInt( request.getParameter("userId") );
		String commodityIds = request.getParameter("commodityIds");
		String commodityCounts = request.getParameter("commodityCounts");
		return userService.addOrder( userId , commodityIds, commodityCounts);
	}
	

	@RequestMapping(value="getOrderList", method = RequestMethod.POST)
	@ResponseBody
	public String getOrderList( HttpServletRequest request, HttpServletResponse response) {
		int userId = Integer.parseInt( request.getParameter("userId") );
		return userDao.getOrderList(userId);
	}
	
	@RequestMapping(value="addForm", method = RequestMethod.POST)
	@ResponseBody
	public boolean addForm( HttpServletRequest request, HttpServletResponse response) {
		int userId = Integer.parseInt( request.getParameter("userId") );
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String totalPrice = request.getParameter("totalPrice");
		String orderList = request.getParameter("orderList");
		String pay = "0";
		return userService.addForm(userId, address, phone, totalPrice, pay, orderList);
	}

	@RequestMapping(value="getFormAllList", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getFormAllList(HttpServletRequest request) {
		return JSONArray.fromObject( userDao.getFormAllList() );
	}
	@RequestMapping(value="getFormList", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getFormList(HttpServletRequest request) {
		return JSONArray.fromObject( userDao.getFormList( Integer.parseInt(request.getParameter("userId")) ) );
	}

	@RequestMapping(value="getFormUnpayList", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getFormUnpayList(HttpServletRequest request) {
		return JSONArray.fromObject( userDao.getFormList( Integer.parseInt(request.getParameter("userId")) , false) );
	}
	
	@RequestMapping(value="getFormPayList", method=RequestMethod.POST)
	@ResponseBody
	public JSONArray getFormPayList(HttpServletRequest request) {
		return JSONArray.fromObject( userDao.getFormList( Integer.parseInt(request.getParameter("userId")) , true) );
	}
	
	@RequestMapping(value="pay", method=RequestMethod.POST)
	@ResponseBody
	public boolean setPaying(HttpServletRequest request) {
		int userId = Integer.parseInt( request.getParameter("userId") );
		int orderformId = Integer.parseInt( request.getParameter("orderformId") );
		return userDao.setPaying(userId , orderformId);
	}
	
	@RequestMapping(value="getInfo", method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getInfo(HttpServletRequest request) {
		int userId = Integer.parseInt( request.getParameter("userId") );
		return JSONObject.fromObject( userDao.getInfo( userId ) );
	}
	
	@RequestMapping(value = "updateInfo", method=RequestMethod.POST)
	@ResponseBody
	public boolean updateInfo( HttpServletRequest request ) {
		String userId = request.getParameter("userId");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String defaultAddress = request.getParameter("defaultAddress");
		String defaultPhone = request.getParameter("defaultPhone");
		String mail = request.getParameter("mail");
		return userDao.updateInfo(userId, name, password, defaultAddress, defaultPhone, mail);
	}

	@RequestMapping(value="addComment", method=RequestMethod.POST)
	@ResponseBody
	public boolean addComment(HttpServletRequest request, HttpServletResponse response) {
		String userId = request.getParameter("userId");
		String userName = request.getParameter("userName");
		String commodityID = request.getParameter("commodityID");
		String comment = request.getParameter("comment");
		return  commentDao.addComment( Integer.parseInt(userId), userName, Integer.parseInt(commodityID), comment );
	}
}
